package com.dba.example.gwt.client;

import java.util.logging.Logger;

import com.dba.example.gwt.client.gin.ExampleGinjector;
import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.place.shared.PlaceChangeEvent;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.HandlerRegistration;

public class Main implements EntryPoint {

	private static final Logger LOG = Logger.getLogger("Main");

	interface DefaultUiBinder extends UiBinder<Widget, Main> {
	}

	private static DefaultUiBinder uiBinder = GWT.create(DefaultUiBinder.class);

	private ExampleGinjector injector = GWT.create(ExampleGinjector.class);

	@SuppressWarnings("unused")
	private PlaceController pc = injector.getPlaceController();

	@UiField
	SimplePanel container;

	@UiField
	Label welcome;

	@UiField
	Anchor home;
	
	@UiField
	Anchor account;
	
	@UiField
	Anchor company;
	
	protected HandlerRegistration placeReg;

	public void onModuleLoad() {
		RootLayoutPanel rp = RootLayoutPanel.get();
		rp.add(uiBinder.createAndBindUi(this));
		container.setHeight((Window.getClientHeight() - 10) + "px");
		container.setWidth((Window.getClientWidth() - 310) + "px");

		ActivityManager activityManager = injector.getActivityManager();
		activityManager.setDisplay(container);

		placeReg = injector.getEventBus().addHandler(PlaceChangeEvent.TYPE,
				new PlaceChangeEvent.Handler() {
					public void onPlaceChange(PlaceChangeEvent event) {
						updateLinks(event.getNewPlace().toString());
					}
				});

		PlaceHistoryHandler historyHandler = injector.getPlaceHistoryHandler();
		historyHandler.handleCurrentHistory();
		
		LOG.fine("Application started successfully");
	}
	
	private void updateLinks(String token) {
		home.getElement().getStyle().setFontWeight(Style.FontWeight.NORMAL);
		account.getElement().getStyle().setFontWeight(Style.FontWeight.NORMAL);
		company.getElement().getStyle().setFontWeight(Style.FontWeight.NORMAL);
		
		if (token.startsWith("home")) {
			home.getElement().getStyle().setFontWeight(Style.FontWeight.BOLD);
		} else if (token.startsWith("account")) {
			account.getElement().getStyle().setFontWeight(Style.FontWeight.BOLD);
		} else if (token.startsWith("company")) {
			company.getElement().getStyle().setFontWeight(Style.FontWeight.BOLD);
		}
	}

}
